Capitalize first and last letters of each wordΒΆ

Capitalize first and last letters of each word of a given string.
def capitalize_first_last_letters(S):

    S = result = S.title()
    result = ""

    for word in S.split():
        result += word[:-1] + word[-1].upper() + " "

    return result[:-1]

# test
print(capitalize_first_last_letters("python exercises practice solution"))
print(capitalize_first_last_letters("w3resource"))

Output:

PythoN ExerciseS PracticE SolutioN
W3ResourcE